package com.li.controller;

import com.li.common.config.Result;
import com.li.mapper.AdviceMapper;
import com.li.mapper.ViolationMapper;
import com.li.pojo.Advice;
import com.li.pojo.Violation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/violation")
public class ViolationController {
  @Autowired
  ViolationMapper adviceMapper;
  
  //获取公告列表
  @GetMapping("")
  public Result<?> getStudents(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer rows) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("offset", ((page - 1) * rows));
    queryMap.put("pageSize", rows);
    ret.put("rows", adviceMapper.findList(queryMap));
    ret.put("total", adviceMapper.getTotal(queryMap));
    return Result.success(ret);
  }
  
  /**
   * 新增
   * @param
   * @return
   */
  @PostMapping
  public Result<?> save(@RequestBody Violation advice) {
  
    try {
      advice.setWtime(new Date());
      //自动更新创作时间
      adviceMapper.add(advice);
      return Result.success();
    }catch (DataAccessException e){
      return Result.error("-1","添加失败!学号错误");
    }
    

  }
  
  /**
   * 更新
   * @param
   * @return
   */
  @PutMapping
  public Result<?> update(@RequestBody Violation advice) {
    adviceMapper.updateById(advice);
    return Result.success();
  }
  
  @DeleteMapping("/{id}")
  public Result<?> deleteUser(@PathVariable Integer id) {
    Integer i = adviceMapper.deleteById(id);
    if (i == 0){
      return Result.error("-1", "删除失败");
    }
    return Result.success();
  }
  
}
